//
//  TBZXDetailViewController.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/28.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "TBZXDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "OrderDetailTableViewController.h"
#import "MWPhotoBrowser.h"

@interface TBZXDetailViewController ()<MWPhotoBrowserDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
@property(nonatomic,weak)IBOutlet UICollectionView *collectionView;
@property(nonatomic,weak)IBOutlet UILabel *pageLabel;
@property(nonatomic,weak)IBOutlet UILabel *nameLabel;
@property(nonatomic,strong)NSDictionary *dataDic;


@end

@implementation TBZXDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendRequestForLoadData];
}

-(void)sendRequestForLoadData{
    NSString *urlString = [NSString stringWithFormat:@"%@/product/detail.do?id=%@",JSXF_HOST,self.dataId];
    
    [SVProgressHUD show];
    
    [ZCWebServiceHelper requestWithMethod:GET andUrlStr:urlString andParameters:nil andFinished:^(id response, NSURLSessionDataTask *dataTask) {
        [SVProgressHUD dismiss];
        if (![response[@"data"] isKindOfClass:[NSString class]]) {
            NSDictionary *dic = response;
            self.dataDic = dic[@"data"];
            [self resetView];
        }
        
    }];
}

-(void)resetView{
    UILabel *nameLabel = self.nameLabel;
    UILabel *currentPriceLabel = [self.view viewWithTag:2];
    UILabel *originPriceLabel = [self.view viewWithTag:3];
    UILabel *descLabel = [self.view viewWithTag:4];
    
    nameLabel.text = [NSString stringWithFormat:@"%@",self.dataDic[@"name"]];
    NSString *descString =[NSString stringWithFormat:@"%@",self.dataDic[@"intro"]];
    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:descString];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:8];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [descString length])];
    [descLabel setAttributedText:attributedString1];
    [descLabel sizeToFit];
    
    currentPriceLabel.text = [NSString stringWithFormat:@"%@",FORMAT_PRICE(self.dataDic[@"currentPrice"])];
    [originPriceLabel setAttributedText:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"原价：¥%@",FORMAT_PRICE(self.dataDic[@"originalPrice"])] attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle|NSUnderlinePatternSolid)}]];
    
    [self.collectionView reloadData];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray *array = self.dataDic[@"dpic"];
    return array.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    UIImageView *imageView = [cell viewWithTag:1];
    NSArray *array = self.dataDic[@"dpic"];
    NSDictionary *dic = array[indexPath.row];
    [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",JSXF_HOST_FILE,dic[@"pic"]]]];

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    UIImageView *imageView = [cell viewWithTag:1];
    MWPhoto *photo = [[MWPhoto alloc]initWithImage:imageView.image];

    
//    NSMutableArray *mArray = [[NSMutableArray alloc]init];
//    NSArray *groupArray = self.dataDic[@"dpic"];
//    for (NSInteger i = 0; i < groupArray.count; i++) {
//        NSDictionary *dic = groupArray[i];
//            MWPhoto *photo = [[MWPhoto alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",JSXF_HOST_FILE,dic[@"pic"]]]];
//            [mArray addObject:photo];
//
//
//    }
    MWPhotoBrowser *pbVC = [[MWPhotoBrowser alloc]initWithPhotos:@[photo]];
//    MWPhotoBrowser *pbVC = [[MWPhotoBrowser alloc]initWithDelegate:self];
    [self.navigationController pushViewController:pbVC animated:YES];
//    [pbVC setCurrentPhotoIndex:indexPath.row];

}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
    NSArray *array = self.dataDic[@"dpic"];
    return array.count;
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
    NSArray *array = self.dataDic[@"dpic"];
    NSDictionary *dic = array[index];
    return [MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",JSXF_HOST_FILE,dic[@"pic"]]]];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    return CGSizeMake(width, width);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat x = scrollView.contentOffset.x;
    NSInteger current = (NSInteger)(x/screenWidth + 0.5f) + 1;
    NSArray *array = self.dataDic[@"dpic"];
    self.pageLabel.text = [NSString stringWithFormat:@"%@/%@",@(current),@(array.count)];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"toOrder"]) {
        OrderDetailTableViewController *vc = segue.destinationViewController;
        vc.dataDic = self.dataDic;
    }
}


@end
